package org.dromara.module.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.module.domain.CnCoupon;
import org.dromara.module.service.ICnCouponService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 积分兑换券
 *
 * @author Lion Li
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/coupon")
public class CnCouponController extends BaseController {

    private final ICnCouponService cnCouponService;

    /**
     * 查询积分兑换券列表
     */
    @SaCheckPermission("module:coupon:list")
    @GetMapping("/list")
    public TableDataInfo<CnCoupon> list(CnCoupon cnCoupon, PageQuery pageQuery) {
        QueryWrapper<CnCoupon> qw = new QueryWrapper<>();

        TableDataInfo<CnCoupon> td = PageUtils.buildDataInfo(cnCouponService.page(PageUtils.buildPage(),qw));
        List<CnCoupon> list = td.getRows();
        return td;
    }

    /**
     * 导出积分兑换券列表
     */
    @SaCheckPermission("module:coupon:export")
    @Log(title = "积分兑换券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CnCoupon cnCoupon, HttpServletResponse response) {
        List<CnCoupon> list = cnCouponService.list();
        ExcelUtil.exportExcel(list, "积分兑换券", CnCoupon.class, response);
    }

    /**
     * 获取积分兑换券详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:coupon:query")
    @GetMapping("/{id}")
    public R<CnCoupon> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        CnCoupon cnCoupon = cnCouponService.getById(id);
        if(cnCoupon != null){

        }
        return R.ok(cnCoupon);
    }

    /**
     * 新增积分兑换券
     */
    @SaCheckPermission("module:coupon:add")
    @Log(title = "积分兑换券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CnCoupon cnCoupon) {
        return toAjax(cnCouponService.save(cnCoupon));
    }

    /**
     * 修改积分兑换券
     */
    @SaCheckPermission("module:coupon:edit")
    @Log(title = "积分兑换券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CnCoupon cnCoupon) {
        return toAjax(cnCouponService.updateById(cnCoupon));
    }

    /**
     * 删除积分兑换券
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:coupon:remove")
    @Log(title = "积分兑换券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(cnCouponService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
